//
//  VisitorLoginView.swift
//  bboySinaWeibo
//
//  Created by apple on 15/11/11.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

protocol VisitorLoginViewDelegate:NSObjectProtocol{
    
    //协议方法 
    //登陆
    func visitorWillLogin()
    
    //注册
    func visitorWillRegister()
    
}
class VisitorLoginView: UIView {

    
    //声明代理属性
    weak var visitorDelegate :VisitorLoginViewDelegate?
    
    //实现注册 登陆按钮的点击方法
    @objc func registerBtnClick(){
        
        print("注册")
        visitorDelegate?.visitorWillRegister()
    }
    //登陆
    @objc func loginBtnClick(){
        
        print("登陆")
        visitorDelegate?.visitorWillLogin()
        
    }
   //每个访客视图页面设置的接口
    
    func setUPInfo (title:String,imageName:String?) {
        
       
        if imageName != nil {
             iconView.hidden = true
            circleView.image = UIImage(named: imageName!)
            bringSubviewToFront(circleView)
        }
        
        tipLabel.text = title
    }
    //重写 init(frame:CGRect)
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUPUI()
    }
    
    private func setUPUI() {
        
        //添加子视图
        
        addSubview(circleView)
        //在这里添加蒙板
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLabel)
        
        addSubview(loginButton)
        
        addSubview(registerButton)
        
        for v in subviews {
            
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //添加布局约束VFL
        //设置circleView
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //设置iconView
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))

        //设置tipLabel
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        
        //设置tipLabel 的宽和高
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 250))
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50))
        
        //设置登陆按钮的约束
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //设置注册按钮的约束 
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute:.Right, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))


        //设置遮罩背景图的约束
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-25)-[registerButton]", options: [], metrics: nil, views: ["backView":backView ,"registerButton": registerButton]))
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加按钮的点击事件
        registerButton.addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        loginButton.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)

        
    }
    //必须实现的方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //懒加载控件
    
    private lazy var circleView: UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return imageView
    }()
    
    private var iconView : UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return imageView
        
    }()
    
    //遮罩背景图
    private var backView : UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return imageView
        
    }()
    
    //tipLabel 
    private var tipLabel : UILabel = {
        
        let l = UILabel()
        
        l.font = UIFont.systemFontOfSize(14)
        
        l.text = "关注一些人 回来看看这里有什么惊喜 关注一些人 回来看看这里有什么惊喜"
        l.textAlignment = NSTextAlignment.Center
        l.numberOfLines = 0

        return l
        
    }()
    //登陆和注册按钮
    private lazy var loginButton: UIButton = {
        
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        btn.setTitle("登陆", forState: UIControlState.Normal)
        return btn
        
    }()
    
    private lazy var registerButton: UIButton = {
        
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        return btn
        
        }()

}
